package com.mixsmart.web.plugins.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.plugins.IPlugin;
import com.mixsmart.core.bean.MixResponse;
import com.mixsmart.core.helper.ObjectHelper;
import com.mixsmart.core.service.IResQueryService;
import com.mixsmart.utils.StringUtils;
import com.mixsmart.web.plugins.bean.JqGridData;
import com.mixsmart.web.plugins.bean.JqGridRows;

/**
 * JqGrid插件服务类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年2月2日
 */
@Service("jqGridPlugin")
public class JqGridService implements IPlugin {

	@Autowired
	private IResQueryService resQueryServ;
	
	@SuppressWarnings("unchecked")
	@Override
	public <E> MixResponse<E> getDatas(String resName,HashMap<String, Object> params) {
		MixResponse<JqGridData> smartResp = new MixResponse<JqGridData>();
		if(StringUtils.isNotEmpty(resName)) {
			MixResponse<Object> mixResp = resQueryServ.getDatas(resName, params);
			if(IWebConstant.OP_SUCCESS.equals(mixResp.getResult())) {
				JqGridData jqGridData = new JqGridData();
				jqGridData.setPage(1);
				jqGridData.setRecords(mixResp.getTotalNum());
				jqGridData.setTotal(1);
				List<Object> objs = mixResp.getDatas();
				if(null != objs && objs.size()>0) {
					List<JqGridRows> jqGridRows = new ArrayList<JqGridRows>();
					JqGridRows jqGridRow = null;
					for (Object obj : objs) {
						Object[] objArray = (Object[])obj;
						jqGridRow = new JqGridRows();
						jqGridRow.setId(StringUtils.handNull(objArray[0]));
						jqGridRow.setCell(obj);
						jqGridRows.add(jqGridRow);
					}
					jqGridData.setRows(jqGridRows);
					jqGridRow = null;
					smartResp.setData(jqGridData);
					smartResp.setResult(IWebConstant.OP_SUCCESS);
					smartResp.setMsg(IWebConstant.OP_SUCCESS_MSG);
					jqGridRows = null;
					jqGridData = null;
				}//if
				objs = null;
			}
		}
		return (MixResponse<E>) smartResp;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <E> MixResponse<E> getDatasPage(String resName, HashMap<String, Object> params, int page, int rows) {
		MixResponse<JqGridData> smartResp = new MixResponse<JqGridData>();
		if(StringUtils.isNotEmpty(resName)) {
			MixResponse<Object> mixResp = resQueryServ.getDatas(resName, params, page, rows);
			if(IWebConstant.OP_SUCCESS.equals(mixResp.getResult())) {
				JqGridData jqGridData = new JqGridData();
				jqGridData.setPage(page);
				jqGridData.setRecords(mixResp.getTotalNum());
				jqGridData.setTotal(mixResp.getTotalPage());
				List<Object> objs = mixResp.getDatas();
				if(null != objs && objs.size()>0) {
					List<JqGridRows> jqGridRows = new ArrayList<JqGridRows>();
					JqGridRows jqGridRow = null;
					objs = ObjectHelper.handleObjDate(objs);
					for (Object obj : objs) {
						Object[] objArray = (Object[])obj;
						jqGridRow = new JqGridRows();
						jqGridRow.setId(StringUtils.handNull(objArray[0]));
						jqGridRow.setCell(obj);
						jqGridRows.add(jqGridRow);
					}
					jqGridData.setRows(jqGridRows);
					jqGridRow = null;
					smartResp.setData(jqGridData);
					smartResp.setResult(IWebConstant.OP_SUCCESS);
					smartResp.setMsg(IWebConstant.OP_SUCCESS_MSG);
					jqGridRows = null;
					jqGridData = null;
				}//if
				objs = null;
			}
		}
		return (MixResponse<E>) smartResp;
	}
	
}
